<template>
  <div class="login-container">
    <a-spin v-if="loading" tip="登录中..." size="large"></a-spin>

    <Empty v-else title="登录出错，请重试" />
  </div>
</template>
<script>
import { dataApi } from '@/api/api'
import { isBlank } from '@/utils/dataType'
import Empty from '@/components/empty'
export default {
  components: {
    Empty
  },
  data () {
    return {
      loading: false,
      codeText: '',
      tokenText: ''
    }
  },
  mounted () {
    this.codeText = this.$route.query.code
    this.codeLogin()
  },
  methods: {
    codeLogin () {
      const params = {}
      if (isBlank(this.codeText)) {
        this.$message.error('参数错误')
      } else {
        params.glc = 'P1HNQ0898005'
        params.rdid = this.codeText
        dataApi.loginByCode(params)
          .then(res => {
            if (res.data.ack) {
              const resData = res.data.data
              window.localStorage.setItem('REC-TOKEN', resData.tokenValue)
              this.$router.replace('/')
            } else {
              this.$message.error(res.data.message)
            }
          })
          .finally(() => {
            this.loading = false
          })
      }
    },
    login () {
      const params = {
        glc: 'P1HNQ0898005',
        rdid: 'testmx05'
      }
      dataApi.login(params)
        .then(res => {
          if (res.data.code === 200) {
            const resData = res.data.data
            window.localStorage.setItem('REC-TOKEN', resData.tokenValue)
            this.$router.push({
              path: '/recommend'
            })
          }
        })
    }
  }
}
</script>
<style lang="scss" scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
